package com.sztx.pay.center.worker.quartz;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.sztx.pay.center.core.service.BillnoService;
import com.sztx.pay.center.core.service.SettleService;
import com.sztx.pay.center.dataaccess.dao.mysql.SettleOrderMysqlDAO;
import com.sztx.pay.center.rpc.api.service.SettleRpcService;
import com.sztx.se.core.quartz.job.BaseJob;
import com.sztx.se.rpc.dubbo.client.DubboClientFactory;

@Component("scanSettleLaterOrderJob")
public class ScanSettleLaterOrderJob extends BaseJob {

	@Autowired
	SettleOrderMysqlDAO settleOrderDao;
	
	@Autowired
	SettleService settleService;
	
	@Autowired
	BillnoService billNoService;

	@Autowired(required = true)
	private DubboClientFactory dubboClientFactory;
	
	@Override
	public Object executeJob(JobExecutionContext arg0) throws JobExecutionException {
		log.info("ScanSettleLaterOrderJob============================START");
//		// 扫描未结算的定时结算订单
//		Calendar cal = Calendar.getInstance();
//		cal.add(Calendar.DAY_OF_MONTH, 1);
//		cal.set(Calendar.HOUR_OF_DAY, 0);
//		cal.set(Calendar.MINUTE, 0);
//		cal.set(Calendar.SECOND, 0);
//		Date settleTime = cal.getTime();
//
//		int count = settleOrderDao.countSettleLaterOrders(settleTime);
//		if (count <= 0) {
//			return null;
//		} else {
//			int pageSize = 1000;
//			int totalPage = (count / pageSize) + 1;
//			for (int pageNo = 1; pageNo <= totalPage; pageNo++) {
//				PageQuery page = new PageQuery(pageNo, pageSize);
//				List<SettleOrderDO> orders = settleOrderDao.getSettleLaterOrders(settleTime, page);
//				
//				log.info(String.format("[%s]前未结算的定时结算订单第%s页共%s", settleTime, pageNo, orders.size()));
//
//				for (SettleOrderDO order : orders) {
//					try {
//					// 修改订单状态至结算中
//					settleOrderDao.updateState(order.getSettleOrderId(), SettleState.SETTLING.getValue());
//					} catch (Exception ex) {
//						log.info(ex.getMessage(), ex);
//						continue;
//					}
//				}
//				try {
//					settleService.transfer(billNoService.nextSettleNo(), orders);
//				} catch (Exception ex) {
//					log.error("ScanSettleLaterOrderJob execute error", ex);
//					continue;
//				}
//			}
			getSettleRpcService().settlingOrders();
			log.info("ScanSettleLaterOrderJob============================END");
			return null;
//		}
	}
	
	protected SettleRpcService getSettleRpcService() {
		return dubboClientFactory.getDubboClient("settleRpcService");
	}

}
